In this lecture, we're going to be scripting the next trap in our RB, which are small tripwires that
can knock you over and cause you to fall to your death.
So let's get started.
So we're just going to follow the exact same procedure that we did for the wood break handler is that
we're going to create a new module script.
The name for this module script is going to be my tripwire handler.
And then I'm going to use the same template.
Once again paste that in here and I'm going to rename this to my tripwire handler.
And we're only going to need an initialize function because we're going to get the collection service
and grab all of the tripwires in our game that have the tag of tripwire.
So I can go ahead and define my tag.
Which is just the string of tripwire.
And then we can go ahead and listen to the collection service, get instance added signal, pass my
tag and then go ahead and get my instance.
And this is where we're going to use our class to create new tripwire objects.
Now inside of my RB model two you can see that I have a tripwire in here tagged with tripwire.
And inside of it it's very simple, just some parts and a detect part that we're going to listen for
when a player touches and when our player touches it, we're going to trip them over.
So we can go ahead and create another new module script that's going to be a child of our tripwire handler.
And we're going to call this one our tripwire class.
And inside of here.
Same deal.
We're going to use our class template and I'm going to name this to tripwire class.
And I'll just rename this table here to Tripwire Objects.
Then we can go ahead and get my class inside of my tripwire handler.
So I'll call it tripwire class.
And that's equal to require script dot tripwire class.
So anytime a new tripwire is added into the workspace, we're going to use the tripwire dot new constructor
and pass that instance.
Inside our tripwire class, we're going to need a couple services.
One is going to be the player service.
And then we're also going to need the sound service.
And that's because we're going to make a reference to a couple of things.
First, we want to get the local player which is equal to players dot local player.
We're going to get the tripwire sound that's inside of the sound service.
So that's in the SFX folder and it's called tripwire.
And then we can go ahead and get a random generator.
So random dot new.
And then we can also define a constant for this class.
And I'm going to call this constant cooldown.
And this is going to represent how long we should wait before the tripwire should be able to trip a
player again.
And we're going to set it to like two seconds.
So that way when the player touches the tripwire, they aren't like tripped multiple times instantaneously,
right?
We want to add a little bit of a cooldown.
Now inside of our constructor we can go ahead and make a reference to our object.
And then we can go ahead and have that same kind of debounce that we had in our wood break class.
So we'll call this on cooldown and by default it's going to be equal to false.
And then we're going to listen for when the detect zone part in our model is touched.
We'll get that other part.
And then we can go ahead and check to see if a player touched it.
So we're going to do players get player from character pass other part dot parent.
And if there isn't a player, then we're just going to return.
Otherwise we can just trip ourselves so we can go ahead and add a function to the meta table.
And we can call this function trip player.
And because this is going to be run on the client, the only player we can really trip here is the local
player.
So you could define a player to be passed to this function.
Or you could just leave it blank and just trip the local player.
By default.
It doesn't really matter whatever you'd like to do, and we're going to be calling this function from
the lambda function connected to the touched event.
So we could do self trip player.
And that means we got to make sure we aren't on a cooldown.
So if self dot cooldown is true then we're just going to return.
Otherwise we can set self to on cooldown equal to true.
And then what we're going to do is we're going to clone that tripwire sound.
We're going to set the parent of that sound equal to, um, we'll set it to the wire part that's inside
of our trip wire.
So that'll be self dot model dot wire.
And then we'll set play on remove equal to true.
And then we will destroy this clone.
So that way it plays a sound.
And then it cleans it up for us automatically.
And then we can go ahead and get the humanoid from our player.
So that would be, uh, local player.
Dot character and find first child, which is a humanoid.
And then we'll also get the root part of our players character.
So that's equal to local player dot character dot humanoid root part.
And this is because we need to change a state in our humanoid.
So we'll use the change state function.
And we want to change it to the humanoid state type of falling down.
So we trip our player and then to basically have them fall forward, we can apply a small force to the
root parts assembly linear velocity.
So root part dot assembly linear velocity.
That's going to be equal to.
Um, we'll actually just add it to itself.
That's going to be equal to the root part dot c frame dot look vector.
And we can multiply this value by whatever we want.
So maybe like a value of 20.
And then what we could do is wait or yield for that cool down to set on, cool down, back to false,
to trip up ourselves again.
That's really all we need to do for this particular tripwire trap.
So any time a tripwire is added to our game because we clone a model and parent it to the workspace,
then we're going to set up that tripwire and trip our local player when they touch that detect zone
apart.
So now we can actually just go and play, test the game and make sure everything works perfectly.
And let's see if we generate any models that have the tripwire inside of it.
So I'll walk around a little bit here and I'll catch you when I find one.
All right.
Here's one of the tripwire models.
And if I go and walk and touch it, as you can see, it knocked me forward right on my face.
And if I go and touch it again, there you go.
I tripped again, so pretty neat.
As we walk forward, it trips us on our face.
And this would be a killer combo if a bridge spawned in front of this tripwire.
So if you did not see this tripwire and there was a bridge in front of you, you would trip fall on
those wood.
Those wood pieces would break and you would fall to your death.
So a cool little hidden trap that's also a little bit annoying for our players.
That's all for this lecture, and I'll catch you in the next one.